import { Component } from "react";
import { connect } from "dva";
import { message } from "antd";

export default class Root extends Component {
  static defaultProps = {
    loading: false,
    ACTIONS: [],
    isLoad: false //初次加载页面时是否需要加载数据
  };
  /**
   * Loading 状态处理机制，仅当 actions 中所有请求状态有其一为true则直接返回true
   * @param {*} actions
   * @param {*} loadEffects
   */
  static getLoadStatus(actions = [], loadEffects) {
    //不符合规则的actions直接返回False
    let loading = false;
    if (actions && actions.length > 0) {
      actions.forEach(ACT => {
        if (loadEffects.effects[ACT.type]) {
          loading = true;
          return;
        }
      });
    }
    return loading;
  }
  /**
   * 获取字典Action，key为自定义返回字段，防止默认返回dictList冲突
   * @param {*} dictId
   * @param {*} key
   */
  static getDictAction(dictId, key) {
    return { type: "common/getDict", payload: { dictId }, key };
  }

  getLoadConfig() {} //配置需要加载的Actions

  constructor(props) {
    super(props);
    //记录List列表Action，用于分页加载
    this._listACTS = [];
    //初始化数据
    if (props.isLoad) {
      this._initLoad();
    }
  }
  /**
   * 页面加载是否需要初始化 ACTIONS 数据
   */
  _initLoad() {
    let { dispatch } = this.props;
    const actions = this.getLoadConfig();
    if (actions && actions.length > 0) {
      actions.forEach(act => {
        dispatch(act);
        //保存List列表Action
        if (act.isList) this._listACTS.push(act);
      });
    }
  }
  /**
   * 请求或更新数据Action动作
   * @param {*} type
   * @param {*} payload
   */
  dispatch(type, payload, callBack) {
    return this.props
      .dispatch({
        type,
        payload
      })
      .then(callBack);
  }
  goRouter(routeName, state) {
    this.props.history.push(routeName);
  }
  /**
   * 跳转ContentView路由器
   * @param {*} menuKey  必填，constant/menusConfig.js配置key
   * @param {*} params 选填，动态改变contentView 属性
   * @param {*} currMenus 选填，需要变更contentView面包屑时使用
   */
  changeRoute(menuKey, params = {}, currMenus) {
    let { dispatch } = this.props;
    if (dispatch)
      dispatch({
        type: "menuRouter/changeMenu",
        payload: { menuKey, params, currMenus }
      });
    else message.warn("please using [connect] component");
  }
  /**
   * 根据type类型获取文字描述
   * @param {*} type  add：新增，edit：编辑，look：详情
   */
  getTypeDesc(type, afterDesc) {
    let desc = null;
    switch (type) {
      case "add":
        desc = "新增" + afterDesc;
        break;
      case "edit":
        desc = "编辑" + afterDesc;
        break;
      case "look":
        desc = afterDesc + "详情";
        break;
      default:
        desc = "未知操作"; //无
    }
    return desc;
  }

  /*********** View *********/
  // renderPage() {} //子类视图复写
  // render() {
  //   return (
  //     <div>
  //       {this.props.loading ? (
  //         <div className={styles.root}>
  //           <Spin tip="加载中..." />
  //         </div>
  //       ) : null}
  //       {this.renderPage()}{" "}
  //     </div>
  //   );
  // }
}

export { connect };
